package com.finger.crowd.mvc.controller;

import com.finger.crowd.constant.CrowdConstant;
import com.finger.crowd.entity.Admin;
import com.finger.crowd.service.api.AdminService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigInteger;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;


    // 分页
    @RequestMapping("/admin/to/page.html")
    public String getPageInfo(
            ModelMap modelMap,
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
    ) {
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "admin-page";
    }

    // 注销
    @RequestMapping("admin/logout/to/page.html")
    public String logoutAdmin(HttpSession session) {
        session.invalidate();
        return "redirect:/admin/login/to/page.html";
    }

    // 登录验证
    @RequestMapping("admin/do/login.html")
    public String adminLogin(@RequestParam("acct_name") String loginAcct,
                             @RequestParam("password") String password,
                             HttpSession session) {
        Admin admin = adminService.getAdminByLoginAccount(loginAcct, password);
        if (admin != null) {
            session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);
        }
        return "redirect:/admin/main/to/page.html";
    }

    // 根据id删除指定用户
    @RequestMapping("admin/delete/to/admin/{id}/{pageNum}/{keyword}.html")
    public String deleteAdmin(@PathVariable("id") Integer id,
                              @PathVariable("pageNum") Integer pageNum,
                              @PathVariable("keyword") String keyword) {
        adminService.removeAdmin(id);
        return "redirect:/admin/to/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }


    @PreAuthorize("hasAuthority('user:save')")
    // 添加用户
    @RequestMapping("admin/add/to/admin-add.html")
    public String addAdmin(Admin admin) {
        adminService.saveAdmin(admin);
        return "redirect:/admin/to/page.html?pageNum=" + Integer.MAX_VALUE;
    }

    // 判断用户名是否存在
    @RequestMapping("/admin/to/isExists.json")
    public void loginAcctIsIxists(@RequestParam("loginAcct") String loginAcct,
                                  HttpServletResponse resp) throws IOException {
        boolean isExists = adminService.queryAdminByLoginAcct(loginAcct);
        if (loginAcct == null || loginAcct.length() == 0) {
            resp.getWriter().write("请输入用户名");
            return;
        }
        if (isExists) {
            resp.getWriter().write(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
        } else {
            resp.getWriter().write("用户名可用");
        }
    }


    //根据id查询用户，之后再跳转到edit页面用户进行回显
    @RequestMapping("admin/id/page.html")
    public ModelAndView queryAdminById(@RequestParam("id") Integer id) {
        Admin admin = adminService.queryAdminById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("admin", admin);
        modelAndView.setViewName("admin-edit");
        return modelAndView;
    }

    @RequestMapping("/admin/update/to/page.html")
    public String updateAdmin(Admin admin,
                              @RequestParam("keyword") String keyword,
                              @RequestParam("pageNum") Integer pageNum
    ) {
        adminService.updateAdmin(admin);
        return "redirect:/admin/to/page.html?keyword=" + keyword + "&pageNum=" + pageNum;
    }
}
